package com.c.ctgproject.ui.review.material

import android.widget.ImageView
import android.widget.LinearLayout
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.bumptech.glide.Glide
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.review.MaterialPhotoEntity
import com.c.ctgproject.mode.MyConstants
import com.c.ctgproject.utils.*
import kotlinx.android.synthetic.main.activity_check_photo.*
import org.jetbrains.anko.margin

class CheckPhotoActivity : BaseActivity() {
    private val mViewModel: CheckPhotoViewModel by lazy { ViewModelProvider(this)[CheckPhotoViewModel::class.java] }
    private val materialNum: String by lazy { intent.getStringExtra(MyConstants.MATERIAL_NUM) }
    override fun event() {
        changeTransparentStatusBar()
        loading.show()
        mViewModel.getPhoto(materialNum)

        mViewModel.result.observe(this, Observer {
            loading.dismiss()
            MyLog.d("查看物料照片:$it")
            val data = initData(it)
            if (data.status == 0) {
                val entity = getGson().fromJson(it, MaterialPhotoEntity::class.java).data
                mContainerLL.removeAllViews()
                entity?.let { list ->
                    list.forEach { item ->
                        val imageView = ImageView(this)
                        val param = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
                        param.margin = 20f.toPx(this)
                        imageView.layoutParams = param
                        Glide.with(this).load(item.picUrl).into(imageView)
                        mContainerLL.addView(imageView)
                    }
                }
            } else {
                showToast(data.msg)
            }
        })


        mViewModel.error.observe(this, Observer {
            loading.dismiss()
            MyLog.d("错误:$it")
            showToast(it)
        })
    }

    override fun click() {

    }

    override fun getLayout() = R.layout.activity_check_photo

}